home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15612 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.7 KB

  1. Path: nntp.teleport.com!usenet
  2. From: GHouck <hksys@teleport.com>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: double-->float... number changes
  5. Date: 20 Apr 1996 05:55:19 GMT
  6. Organization: systems hk
  7. Message-ID: <4l9u87$sq@nadine.teleport.com>
  8. References: <4l3olr$85o@news.magi.com>
  9. NNTP-Posting-Host: ip-pdx18-35.teleport.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.22 (Windows; I; 32bit)
  14.  
  15. mlefebvr@magi.com (Michel L.) wrote:
  16. >I have a number (that I pick up from Sybase), declared as a double,
  17. >it's value is 123.4, I then want to store it in a float variable, but
  18. >the number changes to 123.399994.
  19. >
  20. >float flt_number=0;
  21. >double dbl_number=123.4;
  22. >flt_number = dbl_number;
  23. >
  24. >flt_number is 123.399994.
  25. >
  26. >This messes up calculations later on in program.
  27. >I tried converting the number to a string and using atof() to get a
  28. >float number, but this function returns a double?!, and the subsequent
  29. >conversion to a float produces the same error.
  30. >
  31. >Is there a way to get 123.4 when converting to a float number?
  32. >
  33.  
  34. Michel,
  35.  
  36. Since floats and doubles typically allow 7 and 15 significant digits
  37. respectively, why would you want to convert the numbers to float
  38. for '...calculations later on in the program...'?  Would it not
  39. be more reasonable to maintain the numbers in the form that carries
  40. the greatest precision until the final print/output?  With each
  41. operation, your lessened precision would accumulate a greater total
  42. error (perhaps).  It appears float 123.4, when printed, because it
  43. is promoted to double in the function call, cannot be reasonably
  44. represented in the 9 digits you show, since it was probably only
  45. precise to 7 of them to begin with (approximately).
  46.  
  47. Yours, Geoff Houck
  48.  
  49.